package com.yanqu.road.server.manager.activity.metaspace;

import com.sun.xml.internal.xsom.impl.scd.Iterators;
import com.yanqu.road.dao.impl.activity.metaspace.CrossMetaSpaceRewardNumDaoImpl;
import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.metaspace.CrossMetaSpaceRewardNumData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.activity.MetaSpaceProto;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossMetaSpaceMgr extends TempMgr {

    private static Map<Integer, ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();

    private static Map<Integer, Map<Integer, ActivityConditionInfo>> conditionInfoMap = new ConcurrentHashMap<>();

    private static Map<Integer, Map<Integer, CrossMetaSpaceRewardNumData>> metaSpaceRewardMap = new ConcurrentHashMap<>();

    /**
     * 直接加锁 不管了
     */
    public static synchronized void trigger(int activityId, int conditionId, long serverId, long userId) {
        // 先判断活动在不在
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        if (activityInfo == null) {
            activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
            if (activityInfo == null) {
                return;
            }
            if (activityInfo.getCrossId() == CrossBaseServer.getInstance().getServerId()) {
                loadActivityInfo(activityInfo);

            }
        }
        Map<Integer, ActivityConditionInfo> infoMap = conditionInfoMap.get(activityId);
        if (infoMap == null) {
            return;
        }
        ActivityConditionInfo conditionInfo = infoMap.get(conditionId);
        if (conditionInfo == null) {
            return;
        }

        // 判断condition 在不在
        int numLimit = conditionInfo.getParamList().get(3).intValue();

        //判断人数满了没有
        Map<Integer, CrossMetaSpaceRewardNumData> dataMap = metaSpaceRewardMap.get(activityId);
        if (dataMap == null) {
            dataMap = new ConcurrentHashMap<>();
            metaSpaceRewardMap.put(activityId, dataMap);
        }
        CrossMetaSpaceRewardNumData data = dataMap.get(conditionId);
        if (data == null) {
            data = new CrossMetaSpaceRewardNumData();
            data.setActivityId(activityId);
            data.setConditionId(conditionId);
            data.setNum(0);
            data.setResetTime(System.currentTimeMillis());
            data.setInsertOption();
            dataMap.put(conditionId, data);
        }
        //判断是否需要重置
        long now = System.currentTimeMillis();
        if (data.getResetTime() < now && !DateHelper.isSameDay(now / 1000, data.getResetTime() / 1000)) {
            data.setNum(0);
            data.setResetTime(now);
        }

        if (data.getNum() >= numLimit) {
            return;
        }
        data.setNum(data.getNum() + 1);

        //给区服发奖吧
        MetaSpaceProto.SendRewardMsgToServerMsg.Builder msg = MetaSpaceProto.SendRewardMsgToServerMsg.newBuilder();
        msg.setActivityId(activityId);
        msg.setConditionId(conditionId);
        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_META_SPACE_CROSS_REWARD, msg);
        MessageHelper.sendPacket(serverId,userId,message);
    }

    public static void loadActivityInfo(ActivityInfo activityInfo) {
        int activityId = activityInfo.getActivityId();
        activityInfoMap.put(activityId, activityInfo);
        conditionInfoMap.remove(activityId);
        Map<Integer, ActivityConditionInfo> dataMap = new ConcurrentHashMap<>();
        conditionInfoMap.put(activityId, dataMap);
        Map<Integer, List<ActivityConditionInfo>> infoMap = ActivityBussiness.getOpenCrossRankActivityConditionInfoMap(Collections.singletonList(activityId));
        List<ActivityConditionInfo> infoList = infoMap.get(activityId);
        if (infoList != null) {
            for (ActivityConditionInfo info : infoList) {
                dataMap.put(info.getConditionId(), info);
            }
        }
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.MetaSpace.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        if (activityInfoMap != null) {
            for (ActivityInfo activityInfo : activityInfoMap.values()) {
                loadActivityInfo(activityInfo);
                List<CrossMetaSpaceRewardNumData> list = new CrossMetaSpaceRewardNumDaoImpl().getCrossMetaSpaceRewardNumData(activityInfo.getActivityId());
                for (CrossMetaSpaceRewardNumData data : list) {
                    Map<Integer, CrossMetaSpaceRewardNumData> dataMap = metaSpaceRewardMap.get(activityInfo.getActivityId());
                    if (dataMap == null) {
                        dataMap = new ConcurrentHashMap<>();
                        metaSpaceRewardMap.put(activityInfo.getActivityId(), dataMap);
                    }
                    dataMap.put(data.getConditionId(), data);
                }
            }
        }
        return true;
    }

    @Override
    public boolean save() {
        if (metaSpaceRewardMap != null) {
            for (Map<Integer, CrossMetaSpaceRewardNumData> dataMap : new ArrayList<>(metaSpaceRewardMap.values())) {
                for (CrossMetaSpaceRewardNumData data : new ArrayList<>(dataMap.values())) {
                    if (data.isInsertOption()) {
                        new CrossMetaSpaceRewardNumDaoImpl().add(data);
                    } else if (data.isUpdateOption()) {
                        new CrossMetaSpaceRewardNumDaoImpl().update(data);
                    }
                }
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
